<?php

namespace App\Http\Controllers\Backend;

use App\Exports\OrderExport;
use App\Models\Areas;
use App\Models\Cities;
use App\Models\Order\Order;
use App\Http\Controllers\Controller;
use App\Models\Provinces;
use Carbon\Carbon;
use Illuminate\Http\Response;
use Maatwebsite\Excel\Facades\Excel;

class OrderController extends Controller
{

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $user = \Auth::user();
        if (!$user->hasPermissionTo('订单管理')) {
            return view('error.error');
        }
        $province = Provinces::where('id', '=', request()->get('province_id', ''))->first();
        $city = Cities::where('id', '=', request()->get('city_id', ''))->first();
        $area = Areas::where('id', '=', request()->get('area_id'))->first();
        $orders = Order::with(['author.info', 'item.video'])
            ->whereHas('author.info', function ($query) use ($province, $city, $area) {
                if ($tel = request()->input('tel', '')) {
                    $query->where('tel', '=', $tel);
                }
                if ($province) {
                    $query->where('province', '=', $province->name);
                }
                if ($city) {
                    $query->where('city', '=', $city->name);
                }
                if ($area) {
                    $query->where('privilege', '=', $area->name);
                }
            })
            ->whereHas('item.video', function ($query) {
                if ($title = request()->input('title', '')) {
                    $query->where('title', 'like', '%' . $title . '%');
                }
            })
            ->where(function ($query) {
                if ($number = request()->input('number', '')) {
                    $query->where('number', 'like', '%' . $number . '%');
                }
                $form = request()->input('from', '');
                $to = request()->input('to', '');
                if ($form && $to) {
                    $query->whereBetween('created_at', [
                        Carbon::parse($form)->startOfDay()->toDateTimeString(),
                        Carbon::parse($to)->endOfDay()->toDateTimeString()
                    ]);
                }
                $from_p = request()->input('from_p', 0);
                $to_p = request()->input('to_p', '');
                if ($to_p) {
                    $query->whereBetween('total', [
                        ($from_p * 100),
                        ($to_p * 100)
                    ]);
                }
            })->where('state', Order::ORDER_PAY)
            ->orderBy('created_at', 'desc')
            ->paginate(15);
        $provinces = Provinces::all();
        $citys = Cities::where('province_id', '=', request()->get('province_id', ''))->get();
        $areas = Areas::where('city_id', '=', request()->get('city_id'))->get();
        return view('order.list', [
            'orders' => $orders,
            'provinces' => $provinces,
            'citys' => $citys,
            'areas' => $areas,
        ]);
    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return Response
     */
    public function show($id)
    {
        //
    }

    public function export()
    {
        $user = \Auth::user();
        if (!$user->hasPermissionTo('订单管理')) {
            return view('error.error');
        }
        $province = Provinces::where('id', '=', request()->get('province_id', ''))->first();
        $city = Cities::where('id', '=', request()->get('city_id', ''))->first();
        $area = Areas::where('id', '=', request()->get('area_id'))->first();
        $orders = Order::with(['author.info', 'item.video'])
            ->whereHas('author.info', function ($query) use ($province, $city, $area) {
                if ($tel = request()->input('tel', '')) {
                    $query->where('tel', '=', $tel);
                }
                if ($province) {
                    $query->where('province', '=', $province->name);
                }
                if ($city) {
                    $query->where('city', '=', $city->name);
                }
                if ($area) {
                    $query->where('privilege', '=', $area->name);
                }
            })
            ->whereHas('item.video', function ($query) {
                if ($title = request()->input('title', '')) {
                    $query->where('title', 'like', '%' . $title . '%');
                }
            })
            ->where(function ($query) {
                if ($number = request()->input('number', '')) {
                    $query->where('number', 'like', '%' . $number . '%');
                }
                $form = request()->input('from', '');
                $to = request()->input('to', '');
                if ($form && $to) {
                    $query->whereBetween('created_at', [
                        Carbon::parse($form)->startOfDay()->toDateTimeString(),
                        Carbon::parse($to)->endOfDay()->toDateTimeString()
                    ]);
                }
                $from_p = request()->input('from_p', 0);
                $to_p = request()->input('to_p', '');
                if ($to_p) {
                    $query->whereBetween('total', [
                        ($from_p * 100),
                        ($to_p * 100)
                    ]);
                }
            })->where('state', '=', Order::ORDER_PAY)
            ->orderBy('created_at', 'desc')
            ->get();
        return Excel::download(new OrderExport($orders), 'orders.xlsx');
    }

    public function getCities()
    {
        $cities = Cities::where('province_id', '=', request()->get('province'))->get();
        return json_encode($cities);
    }

    public function getAreas()
    {
        $cities = Areas::where('city_id', '=', request()->get('city_id'))->get();
        return json_encode($cities);
    }
}
